"""
Configuration settings for the emobot project.
"""
import os
from dotenv import load_dotenv
from typing import Optional

# Load environment variables from .env file
load_dotenv()


class Config:
    """Application configuration class."""

    # Image storage settings
    IMAGE_STORAGE_DIR: str = os.path.join(os.getcwd(), "images")
    IMAGE_WEB_PREFIX: str = "/images"

    # Database settings
    DATABASE_URL: str = os.getenv("DATABASE_URL", "sqlite:///./emobot.db")
    DB_HOST: str = os.getenv("DB_HOST", "192.168.5.93")
    DB_PORT: int = int(os.getenv("DB_PORT", "5432"))
    DB_USER: str = os.getenv("DB_USER", "myusr")
    DB_PASSWORD: str = os.getenv("DB_PASSWORD", "pass1234")
    DB_NAME: str = os.getenv("DB_NAME", "emobot")

    # LLM API settings (DashScope)
    LLM_API_KEY: str = os.getenv("LLM_API_KEY", "sk-2c6fe27d93354aa2bc9f8204f4ae2222")
    LLM_BASE_URL: str = os.getenv("LLM_BASE_URL", "https://dashscope.aliyuncs.com/compatible-mode/v1")
    LLM_MODEL: str = os.getenv("LLM_MODEL", "qwen-turbo")  # 从qwen-max改为qwen-turbo以提升响应速度

    # VLM API settings
    VLM_API_KEY: str = os.getenv("VLM_API_KEY", "")
    VLM_API_URL: str = os.getenv("VLM_API_URL", "")

    # ASR API settings (DashScope)
    ASR_API_KEY: str = os.getenv("ASR_API_KEY", "sk-2c6fe27d93354aa2bc9f8204f4ae2222")
    ASR_MODEL: str = os.getenv("ASR_MODEL", "qwen3-asr-flash")

    # Redis settings
    REDIS_URL: str = os.getenv("REDIS_URL", "redis://192.168.5.93:6379/0")
    REDIS_HOST: str = os.getenv("REDIS_HOST", "192.168.5.93")
    REDIS_PORT: int = int(os.getenv("REDIS_PORT", "6379"))

    # Celery settings
    CELERY_BROKER_URL: str = os.getenv("CELERY_BROKER_URL", "redis://192.168.5.93:6379/0")
    CELERY_RESULT_BACKEND: str = os.getenv(
        "CELERY_RESULT_BACKEND", "redis://192.168.5.93:6379/0"
    )

    # Server settings
    PORT: int = int(os.getenv("PORT", "8000"))
    HOST: str = os.getenv("HOST", "0.0.0.0")

    # CORS settings
    CORS_ORIGINS: list = os.getenv("CORS_ORIGINS", "*").split(",")

    # Logging settings
    LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO")

    # InsightFace settings
    INSIGHTFACE_HOME: str = os.getenv("INSIGHTFACE_HOME", "./models")

    # Huawei Cloud OBS settings - 已注释，使用本地存储
    # HUAWEICLOUD_ACCESS_KEY: str = os.getenv("HUAWEICLOUD_ACCESS_KEY", "")
    # HUAWEICLOUD_SECRET_KEY: str = os.getenv("HUAWEICLOUD_SECRET_KEY", "")
    # HUAWEICLOUD_OBS_ENDPOINT: str = os.getenv("HUAWEICLOUD_OBS_ENDPOINT", "")
    # HUAWEICLOUD_OBS_BUCKET: str = os.getenv("HUAWEICLOUD_OBS_BUCKET", "")
    # HUAWEICLOUD_OBS_CDN_ENDPOINT: str = os.getenv("HUAWEICLOUD_OBS_CDN_ENDPOINT", "")

    # Memo service settings (mem0 + weaviate)
    MEMO_LLM_MODEL: str = os.getenv("MEMO_LLM_MODEL", "qwen-turbo")  # 从qwen-max改为qwen-turbo以提升响应速度
    MEMO_LLM_API_KEY: str = os.getenv("MEMO_LLM_API_KEY", "")
    MEMO_LLM_BASE_URL: str = os.getenv("MEMO_LLM_BASE_URL", "")
    MEMO_EMBEDDER_MODEL: str = os.getenv(
        "MEMO_EMBEDDER_MODEL", "text-embedding-3-large"
    )
    MEMO_EMBEDDER_BASE_URL: str = os.getenv("MEMO_EMBEDDER_BASE_URL", "")
    MEMO_EMBEDDER_API_KEY: str = os.getenv("MEMO_EMBEDDER_API_KEY", "")
    MEMO_WEAVIATE_URL: str = os.getenv("MEMO_WEAVIATE_URL", "http://192.168.5.93:8080")
    MEMO_COLLECTION_NAME: str = os.getenv("MEMO_COLLECTION_NAME", "user_memories")

    @classmethod
    def validate(cls) -> bool:
        """Validate configuration settings."""
        required_vars = [
            "DATABASE_URL",
        ]

        missing_vars = []
        for var in required_vars:
            if not getattr(cls, var):
                missing_vars.append(var)

        if missing_vars:
            print(f"Missing required environment variables: {missing_vars}")
            return False

        return True


# Create a global config instance
config = Config()
